package cn.gzsf.sboot07.controller;

import cn.gzsf.sboot07.entity.User;
import cn.gzsf.sboot07.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    private UserMapper mapper;

    @RequestMapping("/check")
    public int check(String username){
        User u = mapper.selectByUsername(username);
        if(u!=null){
            return 2;//已存在
        }
        return 1;//没有存在
    }

    @RequestMapping("/reg")
    public int reg(User user){
        User u = mapper.selectByUsername(user.getUsername());
        if(u!=null){
            return 2;//已存在
        }
        mapper.insert(user);
        return 1;//注册成功
    }
    @RequestMapping("/login")
    public int login(User user){
        System.out.println("user = " + user);
        User u = mapper.selectByUsername(user.getUsername());
        if(u!=null){
            if(u.getPassword().equals(user.getPassword())){
                return 1;//登录成功
            }else {
                return 3;//密码错误
            }
        }
        return 2;//用户名不存在
    }


}
